package com.joney.backstage.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.joney.backstage.constant.Constants;
import com.joney.backstage.dto.UserRoleRsp;
import com.joney.backstage.entity.UserRole;
import com.joney.backstage.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.joney.common.pojo.Result;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 帐户角色表 服务实现类
 * </p>
 *
 * @author joney123
 * @since 2018-02-07
 */
@Service
public class UserRoleService extends ServiceImpl<UserRoleMapper, UserRole>{

    /**
     * 判断是否有对该路径的访问权限
     * @param userId
     * @param permission
     * @return
     */
    public boolean checkPermission(int userId,String permission){
        List<String> list = getPermissions(userId);
        boolean flag = false;
        if(list != null && list.size()>0){
            flag = list.contains(permission);
        }
        return flag;
    }

    public List<String> getPermissions(int userId){
        return baseMapper.getPermissions(userId);
    }

    public List<UserRoleRsp> queryBind(Page page, Map<String,Object> params){
        return baseMapper.queryBind(page,params);
    }

    public List<UserRoleRsp> queryNotBindUser(Page page, Map<String,Object> params){
        return baseMapper.queryNotBindUser(page,params);
    }
    public List<UserRoleRsp> queryNotBindRole(Page page, Map<String,Object> params){
        return baseMapper.queryNotBindRole(page,params);
    }

    public Result bindUser(int roleId, int bindState, String userIds, int operatorId){
        if(roleId == Constants.SUPER_ADMINISTRATOR_ROLE_ID && operatorId != Constants.SUPER_ADMINISTRATOR_ROLE_ID){
            return Result.fail("无权限操作超级管理员的资源");
        }
        String[] userIdArr = userIds.split("-");
        boolean flag = false;
        if(bindState==1){ //绑定
            List<UserRole> list = new ArrayList<>(userIdArr.length);
            for(String userId:userIdArr){
                UserRole ur = new UserRole();
                ur.setRoleId(roleId);
                ur.setUserId(Integer.valueOf(userId));
                ur.setCreateUser(operatorId);
                list.add(ur);
            }
            flag = insertBatch(list);
        }else{ //解绑

            EntityWrapper<UserRole> wrapper = new EntityWrapper<>();
            wrapper.eq("role_id",roleId);
            wrapper.in("user_id",userIdArr);
            flag = delete(wrapper);
        }
        if(!flag){
            return Result.fail("数据更新失败");
        }
        return Result.success();
    }

    public Result bindRole(int userId, int bindState, String roleIds, int operatorId){

        EntityWrapper<UserRole> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id",userId);
        wrapper1.eq("role_id",Constants.SUPER_ADMINISTRATOR_ROLE_ID);
        UserRole hasSuperRole = selectOne(wrapper1);

        if(hasSuperRole != null && operatorId != userId){
            return Result.fail("无权限操作超级管理员的资源");
        }
        String[] roleIdArr = roleIds.split("-");
        boolean flag = false;
        if(bindState==1){ //绑定
            List<UserRole> list = new ArrayList<>(roleIdArr.length);
            for(String roleId:roleIdArr){
                UserRole ur = new UserRole();
                ur.setUserId(userId);
                ur.setRoleId(Integer.valueOf(roleId));
                ur.setCreateUser(operatorId);
                list.add(ur);
            }
            flag = insertBatch(list);
        }else{ //解绑

            EntityWrapper<UserRole> wrapper = new EntityWrapper<>();
            wrapper.eq("user_id",userId);
            wrapper.in("role_id",roleIdArr);
            flag = delete(wrapper);
        }
        if(!flag){
            return Result.fail("数据更新失败");
        }
        return Result.success();
    }

}
